package com.dnp.springbootdocker.controller;

import com.dnp.springbootdocker.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * @author huazai
 * @date 2024/7/3
 */
@Api(tags = "redis测试")
@RestController
@Slf4j
public class RedisController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/add")
    @ApiOperation("添加数据")
    public CommonResult<Void> add(@ApiParam(value = "redisKey", required = true) @RequestParam String key,
                                  @ApiParam(value = "redisValue", required = true) @RequestParam String value) {
        final Boolean b = stringRedisTemplate.opsForValue().setIfAbsent(key, value);
        if (Boolean.TRUE.equals(b)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @GetMapping("/remove")
    @ApiOperation("删除数据")
    public CommonResult<Void> delete(@ApiParam(value = "redisKey", required = true) @RequestParam String key) {
        final Boolean b = stringRedisTemplate.delete(key);
        if (Boolean.TRUE.equals(b)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }
}
